/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.policy;


import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.uml2.uml.Message;

import com.cea.papyrus.diagram.sequence.sedi.editor.message.MessageParser;
import com.cea.papyrus.diagram.sequence.sedi.model.command.DirectEditCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.FindOperationCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.FindSignalCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.SetSignatureCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;

/**
 * @author Andreas Limyr
 * @author frankd@ifi.uio.no
 */
public class MessageDirectEditPolicy extends LabelDirectEditPolicy {

    @SuppressWarnings("unchecked")
    @Override
    protected Command getDirectEditCommand(DirectEditRequest request) {
        final GraphEdge messageView = (GraphEdge) getHost().getModel();
        final Message message = ModelUtil.<Message> getSemantic(messageView);

        final String usertext = (String) request.getCellEditor().getValue();

        final MessageParser.Result parseResult = MessageParser.parse(usertext);

        DirectEditCommand renameCmd = new DirectEditCommand();
        renameCmd.setNamedElement(message);
        renameCmd.setName(parseResult.signalOrOperationName);

        final FindSignalCommand findSignalCmd = new FindSignalCommand();
        findSignalCmd.setContext(message.getNamespace());
        findSignalCmd.setName(parseResult.signalOrOperationName);

        final FindOperationCommand findOperationCmd = new FindOperationCommand();
        findOperationCmd.setContext(message.getNamespace());
        findOperationCmd.setName(parseResult.signalOrOperationName);

        final SetSignatureCommand setSignatureCmd = new SetSignatureCommand();
        setSignatureCmd.setMessage(message);
        setSignatureCmd.setArgumentValues(parseResult.argumentValue);

        final Command glue1 = new Command() {
            public void execute() {
                if (findSignalCmd.getSignal() != null)
                    setSignatureCmd.setSignature(findSignalCmd.getSignal());
                else
                    setSignatureCmd.setSignature(findOperationCmd.getOperation());
            }
        };

        CompoundCommand cmd = new CompoundCommand();
        cmd.setLabel("Edit Message");
        cmd.add(renameCmd);
        cmd.add(findSignalCmd);
        cmd.add(findOperationCmd);
        cmd.add(glue1);
        cmd.add(setSignatureCmd);
        return cmd;
    }

    @Override
    protected void showCurrentEditValue(DirectEditRequest request) {
        String text = (String) request.getCellEditor().getValue();
        GraphicalEditPart host = (GraphicalEditPart) getHost();
        IFigure figure = host.getFigure();
        Label label = (Label) figure.getChildren().get(0);
        label.setText(text);
        figure.getUpdateManager().performUpdate(); // TODO Can this line be removed?
    }
}
